﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace SMSConsulting.SMSLIB.Utilities
{
    /// <summary>
    /// Simple log writer
    /// </summary>
    public class SimpleLogWriter
    {
        private static string _logFullPath;
        public static string LogFullPath
        {
            //= "LogFile.log";
            get
            {
                if (String.IsNullOrEmpty(_logFullPath))
                    _logFullPath = Context.ExecutingAssemblyFolderName +
                        @"\Logs\LogFile.log";
                return _logFullPath;
            }
            set { _logFullPath = value; }
        }

        /// <summary>
        /// Appends log file line
        /// </summary>
        /// <param name="message"></param>
        private static object _locker = new object();
        public static void WriteLogLine(string message)
        {
            try
            {
                string logFullPath = LogFullPath;
                createLogFolder(logFullPath);
                if (System.IO.File.Exists(logFullPath))
                {
                    long logSize = (new System.IO.FileInfo(logFullPath)).Length;
                    if (logSize > 1024 * 1024)
                    {
                        lock (_locker)
                        {
                            string logFullPath1 = logFullPath + ".bak";
                            System.IO.File.Copy(logFullPath, logFullPath1, true);
                            //append = false;
                            System.IO.File.WriteAllText(logFullPath, "");
                        }
                    }
                }
                message = string.Format("{0:dd/MM/yyyy HH:mm:ss} - ", DateTime.Now) +
                         message + System.Environment.NewLine;

                lock (_locker)
                {
                    System.IO.File.AppendAllText(logFullPath, message);
                }

                System.Console.Write("LOG: " + message);

            }
            catch (Exception ex)
            {
                string logFullPath1 = LogFullPath + ".err.txt";
                System.IO.File.WriteAllText(logFullPath1, ex.Message);
            }
        }

        private static string _webLogFullPath;
        public static string WebLogFullPath
        {
            //= "WebLogFile.log";
            get
            {
                if (String.IsNullOrEmpty(_webLogFullPath))
                    _webLogFullPath = Context.WebLogsFolder +
                        @"\WebLogFile.log";
                return _webLogFullPath;
            }
            set { _webLogFullPath = value; }
        }

        private static object _webLogLocker = new object();
        public static void WriteWebLogLine(string message)
        {
            try
            {
                string logFullPath = WebLogFullPath;
                createLogFolder(logFullPath); 
                if (System.IO.File.Exists(logFullPath))
                {
                    long logSize = (new System.IO.FileInfo(logFullPath)).Length;
                    if (logSize > 1024 * 1024)
                    {
                        lock (_webLogLocker)
                        {
                            string logFullPath1 = logFullPath + ".bak";
                            System.IO.File.Copy(logFullPath, logFullPath1, true);
                            System.IO.File.WriteAllText(logFullPath, "");
                        }
                    }
                }
                message = string.Format("{0:dd/MM/yyyy HH:mm:ss} - ", DateTime.Now) +
                         message + System.Environment.NewLine;

                lock (_webLogLocker)
                {
                    System.IO.File.AppendAllText(logFullPath, message);
                }

                System.Console.Write("LOG: " + message);

            }
            catch (Exception ex)
            {
                string logFullPath1 = WebLogFullPath + ".err.txt";
                System.IO.File.WriteAllText(logFullPath1, ex.Message);
            }
        }

        private static void createLogFolder(string fullPath)
        {
            string logFolder = System.IO.Path.GetDirectoryName(fullPath);

            if (!System.IO.Directory.Exists(logFolder))
                System.IO.Directory.CreateDirectory(logFolder);     

        }


    }
}
